package cn.sinjet.carassist;


import com.mgroup.wazehelper.R;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import cn.sinjet.viewmodel.ViewEvent;
import cn.sinjet.viewmodel.ViewModel;

public class PasswordPannel extends Dialog implements android.view.View.OnClickListener{
    ViewGroup mViewRoot;
    Context context;
    String  mPassword="";
    TextView[] vPassword = new TextView[6];
	public PasswordPannel(Context context, int theme) {
		super(context, theme);
		// TODO Auto-generated constructor stub
		this.context = context;
	}

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    	// TODO Auto-generated method stub
    	super.onCreate(savedInstanceState);
    	mViewRoot = (ViewGroup)LayoutInflater.from(context).inflate(R.layout.layout_password_dialog,null);
    	setContentView(mViewRoot);
    	vPassword[0] = (TextView)mViewRoot.findViewById(R.id.psw1);
    	vPassword[1] = (TextView)mViewRoot.findViewById(R.id.psw2);
    	vPassword[2] = (TextView)mViewRoot.findViewById(R.id.psw3);
    	vPassword[3] = (TextView)mViewRoot.findViewById(R.id.psw4);
    	vPassword[4] = (TextView)mViewRoot.findViewById(R.id.psw5);
    	vPassword[5] = (TextView)mViewRoot.findViewById(R.id.psw6);
    	
    	mViewRoot.findViewById(R.id.btn_0).setOnClickListener(this);
    	mViewRoot.findViewById(R.id.btn_1).setOnClickListener(this);
    	mViewRoot.findViewById(R.id.btn_2).setOnClickListener(this);
    	mViewRoot.findViewById(R.id.btn_3).setOnClickListener(this);
    	mViewRoot.findViewById(R.id.btn_4).setOnClickListener(this);
    	mViewRoot.findViewById(R.id.btn_5).setOnClickListener(this);
    	mViewRoot.findViewById(R.id.btn_6).setOnClickListener(this);
    	mViewRoot.findViewById(R.id.btn_7).setOnClickListener(this);
    	mViewRoot.findViewById(R.id.btn_8).setOnClickListener(this);
    	mViewRoot.findViewById(R.id.btn_9).setOnClickListener(this);
    	mViewRoot.findViewById(R.id.btn_cancel).setOnClickListener(this);
    	mViewRoot.findViewById(R.id.btn_del).setOnClickListener(this);
    	
    }

    
	@Override
	public void onClick(View view) {
		// TODO Auto-generated method stub
		switch(view.getId()){
		case R.id.btn_0:
			onClkNum("0");
			break;
		case R.id.btn_1:
			onClkNum("1");
			break;
		case R.id.btn_2:
			onClkNum("2");
			break;
		case R.id.btn_3:
			onClkNum("3");
			break;
		case R.id.btn_4:
			onClkNum("4");
			break;
		case R.id.btn_5:
			onClkNum("5");
			break;
		case R.id.btn_6:
			onClkNum("6");
			break;
		case R.id.btn_7:
			onClkNum("7");
			break;
		case R.id.btn_8:
			onClkNum("8");
			break;
		case R.id.btn_9:
			onClkNum("9");
			break;
		case R.id.btn_cancel:
//			onClkNum("0");
			this.dismiss();
			break;
		case R.id.btn_del:
			if(mPassword.length()>0){
				mPassword = mPassword.substring(0, mPassword.length()-1);
				refreshPswView(mPassword.length()); //vPassword.setText(mPassword);
			}
			break;
		}
	}
    
    private void onClkNum(String strNum){
    	if(mPassword.length()>=6)
    		return;
    	mPassword += strNum;
//    	vPassword.setText(mPassword);
    	refreshPswView(mPassword.length());
    	if(mPassword.length()==6){
    		ViewEvent event = new ViewEvent(R.layout.layout_password_dialog, ViewEvent.CLICK);
    		event.setString(mPassword);
    		ViewModel.getIns().onViewEvent(event);
    		mPassword = "";
//    		vPassword.setText(mPassword);
    		refreshPswView(mPassword.length());
    		this.dismiss();
    	}
    }
    public void setControlHint(String strHint){
    	
    }
    private void refreshPswView(int len){
    	for(int i=0;i<6;i++){
    		if(i<len) vPassword[i].setText("*");
    		else vPassword[i].setText("");
    	}
    }
}
